package com.example.mymusicdisplay.view.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.databinding.BannerImageBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * author: ts
 * created on: 2021/10/9 下午2:27
 * description:
 */
public class BannerAdapter extends PagerAdapter {

    private List<String> mImageList = new ArrayList<>();
    private static final String TAG = "BannerAdapter";

    private BannerImageBinding mImageBinding;
    private RequestOptions mRequestOptions;

    private Context mContext;

    public BannerAdapter(Context context, List<String> imageList) {
        mRequestOptions = new RequestOptions()
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.error)
            .fallback(R.drawable.empty)
            .centerInside();
        mContext = context;
        mImageList = imageList;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        mImageBinding = DataBindingUtil.inflate(
            LayoutInflater.from(mContext), R.layout.banner_image, container, false);
        Glide.with(mContext)
            .load(mImageList.get(position))
            .apply(mRequestOptions)
            .into(mImageBinding.bannerImage);
        container.addView(mImageBinding.getRoot(), 0);
        Log.d(TAG, "instantiateItem: position = " + position);
        return mImageBinding.getRoot();
    }

    @Override
    public int getCount() {
        return mImageList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
